package com.novaed.game.lifeline;

import javax.swing.SwingUtilities;

import com.novaed.game.frames.WindowMain;

/**
 * @author  Glenn Mark
 */
public class TurtleTimer implements Runnable {

	private String name;
	private Thread t;
	
	//TODO #issue 6 @ com-novaed-game Update Timer
	private int START_TIME= 3;
	private boolean suspendFlag;
	
	private WindowMain mw = new WindowMain();
	
	public TurtleTimer (String threadname) {
		name = threadname;
		t = new Thread(this, name);
		System.out.println("New " + t);
		suspendFlag = false;
		t.start(); 
	}

	public void run() {
		
		try {
			
			for (int i = START_TIME; i >= 0; i--) {
				Thread.sleep(1000);
				mw.getTimerLabel().setText(String.valueOf(i));
				System.out.println(i);
				synchronized (this) {
					
					while (suspendFlag) {
						wait();
					}
					
				}
			}
			
			// TODO 0-end
			
		} catch (InterruptedException e) {
			System.err.println(name + " has been interrupted.");
		}
	}

	public void suspendTimer() {
		
		suspendFlag = true;
	}

	public synchronized void resumeTimer() {
		
		suspendFlag = false;
		notify();
	}
	
}